﻿using System;
using System.Collections.Generic;
using System.Text;
using LucasCode.Tools;
using System.Collections.Specialized;
using System.IO;
using LucasCode.Win32;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Diagnostics;
using ESLReminder.Modules;

namespace ESLReminder.Modules.WarnModules
{
    public class TeamChatWarnModule : WarnModule
    {
        private GameInteropModule m_InterOp = null;
        public override PluginInfo CreateInfo()
        {
            return new PluginInfo(
                "Teamchat Text Warnings",
                new Version(1, 0),
                "©2009-2010 Lucas Romero",
                "http://code.google.com/p/quicktools/");
        }
        public override string Description
        {
            get
            {
                return "Warns in teamchat";
            }
        }

        public TeamChatWarnModule()
            : base(5000)
        {
            
        }
        
        public override WarnModuleTypes Type
        {
            get { return WarnModuleTypes.TeamChatWarnModule; }
        }

        public override void OtherPluginLoaded(IPlugin plugin)
        {
            if (plugin.GetType() == typeof(GameInteropModule))
                m_InterOp = (GameInteropModule)plugin;
            base.OtherPluginLoaded(plugin);
        }

        protected override void Warn()
        {
            if (m_InterOp != null && m_InterOp.GameWindowHandle != IntPtr.Zero)
            {
                if (User32.GetForegroundWindow() == m_InterOp.GameWindowHandle)
                    m_InterOp.ExecHLCommand(@"say_team [ " + m_Warning + " ]");
            }
        }

        public override void Test()
        {
            throw new NotSupportedException(Info.TestingNotSupported);
        }

        protected string m_Warning = string.Empty;

        protected override void EventsChanged(WarnEvents newActiveEvents)
        {
            StringBuilder b = new StringBuilder();
            if ((newActiveEvents & WarnEvents.NoAequitas) != WarnEvents.None)
            {
                b.Append("no aequitas / ");
            }
            if ((newActiveEvents & WarnEvents.NoReplay) != WarnEvents.None)
            {
                b.Append("no demo / ");
            }
            if ((newActiveEvents & WarnEvents.TakeStatus) != WarnEvents.None)
            {
                b.Append("no status / ");
            }
            if ((newActiveEvents & WarnEvents.RestartReplay) != WarnEvents.None)
            {
                b.Append("teamchange / ");
            }

            m_Warning = b.ToString();
            if (m_Warning.EndsWith(" / "))
                m_Warning = m_Warning.Remove(m_Warning.Length - 3, 3);

            base.EventsChanged(newActiveEvents);
        }
    }
}
